package com.ruoyi.lxy.controller;

import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.helper.LoginHelper;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lxy.domain.vo.TTimeVo;
import com.ruoyi.lxy.domain.bo.TTimeBo;
import com.ruoyi.lxy.service.ITTimeService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 时间段
 *
 * @author ruoyi
 * @date 2023-08-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lxy/time")
public class TTimeController extends BaseController {

    private final ITTimeService iTTimeService;

    /**
     * 查询时间段列表
     */
    @SaCheckPermission("lxy:time:list")
    @GetMapping("/list")
    public TableDataInfo<TTimeVo> list(TTimeBo bo, PageQuery pageQuery) {
        return iTTimeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出时间段列表
     */
    @SaCheckPermission("lxy:time:export")
    @Log(title = "时间段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TTimeBo bo, HttpServletResponse response) {
        List<TTimeVo> list = iTTimeService.queryList(bo);
        ExcelUtil.exportExcel(list, "时间段", TTimeVo.class, response);
    }

    /**
     * 获取时间段详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("lxy:time:query")
    @GetMapping("/{id}")
    public R<TTimeVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable Long id) {
        return R.ok(iTTimeService.queryById(id));
    }

    /**
     * 新增时间段
     */
    @SaCheckPermission("lxy:time:add")
    @Log(title = "时间段", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TTimeBo bo) {
        return toAjax(iTTimeService.insertByBo(bo));
    }

    /**
     * 修改时间段
     */
    @SaCheckPermission("lxy:time:edit")
    @Log(title = "时间段", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TTimeBo bo) {
        return toAjax(iTTimeService.updateByBo(bo));
    }

    /**
     * 删除时间段
     *
     * @param ids 主键串
     */
    @SaCheckPermission("lxy:time:remove")
    @Log(title = "时间段", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTTimeService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    /**
     * 查询未成行时间段列表
     */
    @SaCheckPermission("lxy:time:list")
    @GetMapping("/list/notcango/{projectId}")
    public TableDataInfo<TTimeVo> NotCanGoList(@PathVariable Long projectId, TTimeBo bo, PageQuery pageQuery) {
        return iTTimeService.queryPageNotCanGoList(projectId, bo, pageQuery);
    }

    /**
     * 查询成行时间段列表
     */
    @SaCheckPermission("lxy:time:list")
    @GetMapping("/list/cango/{projectId}")
    public TableDataInfo<TTimeVo> CanGoList(@PathVariable Long projectId, TTimeBo bo, PageQuery pageQuery) {
        return iTTimeService.queryPageCanGoList(projectId, bo, pageQuery);
    }

    /**
     * 时间段设置成行
     *
     * @param ids 主键串
     */
    @SaCheckPermission("lxy:time:edit")
    @Log(title = "时间段管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("cango/{ids}")
    public R<Void> setCanGo(@NotEmpty(message = "主键不能为空")
                            @PathVariable Long[] ids) {
        return toAjax(iTTimeService.setCanGoByIds(Arrays.asList(ids)));
    }

    /**
     * 时间段取消成行
     *
     * @param ids 主键串
     */
    @SaCheckPermission("lxy:time:edit")
    @Log(title = "时间段管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/notcango/{ids}")
    public R<Void> setNotCanGo(@NotEmpty(message = "主键不能为空")
                               @PathVariable Long[] ids) {
        return toAjax(iTTimeService.setNotCanGoByIds(Arrays.asList(ids)));
    }


    /**
     * 时间段开启补报设置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("lxy:time:edit")
    @Log(title = "时间段管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/reselect/{ids}")
    public R<Void> ReselectTime(String reselectStart, String reselectEnd, @NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iTTimeService.ReselectTime(reselectStart, reselectEnd, Arrays.asList(ids)));
    }

    /**
     * 清空未成形路线，释放报名人员名单
     *
     */
    @SaCheckPermission("lxy:time:remove")
    @Log(title = "时间段", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeNotcango/{projectId}")
    public R<Void> removeNotcango(@PathVariable Long projectId) {
        return toAjax(iTTimeService.removeNotcango(projectId));
    }

}
